package org.example.dao;

import org.apache.ibatis.annotations.*;
import org.example.domain.Model;

import java.util.List;

/**
 * @author Jimmy
 * @version 1.0
 */
public interface ModelDao {

    /**
     * 根据角色id返回模块id
     * @param rid 角色id
     * @return list
     */
    @Select("select module_id from ss_role_module where role_id=#{rid}")
    List<String> findByRid(@Param("rid") String rid);

    /**
     * 根据模块id查找模块信息
     * @param modelId 模块id
     * @return model
     */
    @Select("select module_id modelId,parent_id pid,name modelName,parent_name pName,curl path,remark modelRemark from ss_module where module_id=#{modelId}")
    @ResultType(Model.class)
    Model findByMid(String modelId);

    /**
     * 分页查询
     * @param model model
     * @return list
     */
    @Select("&ltscript&gtselect module_id modelId,name modelName,parent_id pid,parent_name pName,curl path,remark modelRemark,state " +
            "from ss_module " +
            "where 1=1 " +
            "$ltif test= 'moduleId != null '$gt and module_id like concat(concat('%',#{moduleId}),'%') $lt/if$gt" +
                "$ltif test= 'modelName != null '$gt and name like concat(concat('%',#{modelName}),'%') $lt/if$gt" +
            "$ltif test= 'pName != null '$gt and parent_name like concat(concat('%',#{pName}),'%') $lt/if$gt" +
            "$lt/script$gt")
    @ResultType(Model.class)
    List<Model> findByPage(Model model);

    /**
     * 查询总记录数
     * @param model model
     * @return int
     */
    @Select("$ltscript$gtselect count(*) from ss_module " +
            "where 1=1 " +
            "$ltif test= 'moduleId != null '$gt and module_id like concat(concat('%',#{moduleId}),'%') $lt/if$gt" +
            "$ltif test= 'modelName != null '$gt and name like concat(concat('%',#{modelName}),'%') $lt/if$gt" +
            "$ltif test= 'pName != null '$gt and parent_name like concat(concat('%',#{pName}),'%') $lt/if$gt" +
            "$ltscript$gt")
    int findTotal(Model model);

    /**
     * 添加模块
     * @param model 新增模块对象
     * @return int
     */
    @Insert("insert into ss_module(module_id,parent_id,name,curl,state,remark) " +
            "values(#{modelId},#{pid},#{modelName},#{path},#{state},#{modelRemark})")
    int addModel(Model model);

    /**
     * 修改模块信息
     * @param model model对象
     * @return int
     */
    @Update("$ltscript$gt update ss_module" +
            "$ltset$gt" +
            "   $ltif test='modelName != null'$gt name=#{modelName}, $lt/if$gt" +
            "   $ltif test='pid != null'$gt parent_id=#{pid}, $lt/if$gt" +
            "   $ltif test='path != null'$gt curl=#{path}, $lt/if$gt" +
            "   $ltif test='modelRemark != null'$gt remark=#{modelRemark}, $lt/if$gt" +
            "   $ltif test='state != null'$gt state=#{state} $lt/if$gt" +
            "$lt/set$gt" +
            " where module_id=#{modelId}" +
            "$lt/script$gt")
    int update(Model model);

    /**
     * 删除模块信息
     * @param modelId 模块id
     * @return int
     */
    @Delete("delete from ss_module where module_id=#{modelId}")
    int delete(String modelId);


}
